const api = require("../../../../utils/cloudAPIUtils");
const app = getApp();

Page({

    /**
     * 页面的初始数据
     */
    data: {
        deviceSn:null,
        devInfo:{},
        modalName:null,
        toLease:false,
        agreement:{
            title:'租赁服务协议',
            checked:false
        },
        timeCount:0
    },

    _checkOpenSlotResult:function(messageId){
        const _self = this;
        api.post(api.url.bxscnOpenSlotResult,{
            deviceSn:_self.data.deviceSn,
            messageId:messageId
        },res=>{
            if(res.data.code == 200){
                if(res.data.body.resultCode == 0){
                    _self.data.timeCount =  _self.data.timeCount + 1;
                    if( _self.data.timeCount < 15){
                        setTimeout(function(){
                            _self._checkOpenSlotResult(messageId);
                        },1000);
                    }else{
                        wx.hideLoading();
                        wx.showModal({
                          content: '开仓失败，请检查设备是否有空仓或稍后再试',
                          showCancel:false
                        })
                    }
                }else if(res.data.body.resultCode == 110){
                    wx.hideLoading();
                    wx.showModal({
                        content: '开仓失败，错误码：110',
                        showCancel:false
                    })
                }else{
                    wx.hideLoading();
                    wx.showModal({
                        content: res.data.body.slotNum + "号仓室已打开，请将电源放入并插好电源和连接线",
                        showCancel:false
                    })
                }
            }else if(res.data.msg){
                wx.hideLoading();
                wx.showModal({
                  content: res.data.msg,
                  showCancel:false
                })
            }else {
                wx.hideLoading();
                wx.showToast({
                  title: '操作失败',
                  icon:'error'
                })
            }
        })
    },

    _agreementInfo:function(){
        wx.showLoading({
          title: '加载中...',
        })
        const _self = this;
        api.post(api.url.agreementInfo,{
            appId:wx.getAccountInfoSync().miniProgram.appId,
            code:'RentAgreement'
        },res=>{
            wx.hideLoading();
            if(res.data.code == 200){
                var agreement = _self.data.agreement;
                agreement.title = res.data.body.title;
                agreement.content = res.data.body.content;
                _self.setData({
                    agreement:agreement,
                    modalName:'agreement'
                });
            }else if(res.data.msg){
                wx.showModal({
                  content: res.data.msg,
                  showCancel:false
                })
            }else {
                wx.showToast({
                  title: '获取失败',
                  icon:'error'
                })
            }
        })
    },

    //勾选授权
    btnChangeRadio(){
        const agreement = this.data.agreement;
        agreement.checked = !agreement.checked;
        this.setData({agreement});
    },

    btnPay:function(){
        const self = this;
        if(!this.data.agreement.checked){
            this.data.toLease = true;
            this.btnAgreementInfo();
            return
        }
        api.post(api.url.bxscnPay,{
            payType:3,
            appId:wx.getAccountInfoSync().miniProgram.appId,
            deviceSn:this.data.deviceSn
        },(res)=>{
            if(res.data.code==200){
                const tradeNo = res.data.body.tradeNo;
                const payInfo = JSON.parse(res.data.body.payInfo);
                wx.requestPayment({
                    appId:payInfo.appId,
                    nonceStr: payInfo.nonceStr,
                    package: payInfo.package,
                    paySign: payInfo.sign,
                    timeStamp: payInfo.timeStamp+"",
                    signType:payInfo.signType,
                    success(res){
                        console.log(res)
                        self.toUnderway(tradeNo);
                    },fail(err){
                        wx.showToast({
                            title: '支付失败',
                            icon:'error',
                            mask:true
                        })
                    }
                })
            }else if(res.data.msg){
                wx.showModal({
                    content: res.data.msg,
                    showCancel:false,
                    complete: () => {
                        wx.reLaunch({
                            url: '/pages/index/index',
                        })
                    }
                })
            }else {
                wx.showToast({
                  title: '支付失败',
                  icon:'error',
                  mask:true
                })
            }
        })
    },

    btnRevert:function(){
        const _self = this;
        wx.showModal({
          title: '温馨提示',
          content: '请确定是本会员租借了电池或授权归还电池！',
          complete: (res) => {
            if (res.confirm) {
                wx.showLoading({
                  title: '处理中...',
                })
                api.post(api.url.bxscnOpenSlot,{
                    deviceSn:_self.data.deviceSn
                },res=>{
                    wx.hideLoading();
                    if(res.data.code == 200){
                        _self.data.timeCount = 0;
                        _self._checkOpenSlotResult(res.data.body.messageId);
                    }else if(res.data.msg){
                        wx.showModal({
                          content: res.data.msg,
                          showCancel:false
                        })
                    }else {
                        wx.showToast({
                          title: '操作失败',
                        })
                    }
                })
            }
          }
        })
    },

    btnAgreementInfo:function(){
        if(this.data.agreement.content){
            this.setData({
                modalName:'agreement'
            })
        }else{
            this._agreementInfo();
        }
    },

    btnHideModal:function(){
        this.setData({
            modalName:null
        })
    },

    btnChoiceAgreement:function(){
        const agreement = this.data.agreement;
        agreement.checked = true;
        this.setData({
            agreement:agreement,
            modalName:null
        });
        if(this.data.toLease){
            this.btnPay();
        }
    },

    toUnderway(tradeNo){
        console.log(tradeNo);
        wx.redirectTo({
          url: './result?tradeNo='+tradeNo
        })
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if(options.deviceSn){
            this.setData({
                deviceSn:options.deviceSn
            })
        }
        const _self = this;
        api.post(api.url.bxscnScan,{
            appId:wx.getAccountInfoSync().miniProgram.appId,
            deviceSn:options.deviceSn
        },res=>{
            if(res.data.code == 200){
                let agreement = _self.data.agreement;
                if(res.data.body.signingAgreement){
                    agreement.checked = true;
                }
                this.setData({
                    devInfo:res.data.body,
                    agreement:agreement
                })
                if(res.data.body.state == 2){
                    wx.showModal({
                        content: '设备离线',
                        showCancel:false,
                        success(){
                            wx.reLaunch({
                                url: '/pages/index/index',
                            })
                        }
                    })
                }
            }else if(res.data.msg){
                wx.showModal({
                  content: res.data.msg,
                  showCancel:false,
                  success(){
                    wx.reLaunch({
                        url: '/pages/index/index',
                    })
                  }
                })
            }else {
                wx.showToast({
                  title: '获取信息失败',
                })
                setTimeout(() => {
                    wx.reLaunch({
                        url: '/pages/index/index',
                    })
                }, 1000);
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})